<?php

defined('BASEPATH') or die('No direct script access allowed');

/**
 * 账户模型
 * @author  Karson
 * @name    account.php
 * @since   2012-12-13 23:06:46
 */
class account_Model extends Model {

    var $allaccount = array();
    var $account_type = array();
    var $account_currency = array();

    function _init() {
        $this->account_type = array(
            'cash' => '现金账户',
            'bank' => '银行卡/存折',
            'credit' => '信用卡',
            'elec' => '电子账户',
            'other' => '其它账户',
        );
        $account_currency = $this->cache->get("account_currency");
        if (!$account_currency) {
            $account_currency = array();
            $this->db->query("SELECT * FROM {pre}currency ORDER BY id ASC");
            while ($row = $this->db->get_array()) {
                $account_currency[$row['id']] = $row;
            }
            $this->cache->set("account_currency", $account_currency);
        }
        $this->account_currency = $account_currency;
    }

    //读取会员相关的分类
    function load_user_account($user_id = null) {
        $user_id = !is_null($user_id) ? $user_id : $this->auth->user_id;
        $this->db->query("SELECT * FROM {pre}account WHERE user_id='{$user_id}' AND state='1' ORDER BY weigh ASC");
        $allaccount = array();
        while ($row = $this->db->get_array()) {
            $allaccount[$row['id']] = $row;
        }
        $this->allaccount = $allaccount;
        return $allaccount;
    }

    //加载家庭帐户信息
    function load_family_account($family_id) {
        $this->db->query("SELECT * FROM {pre}account WHERE user_id IN (SELECT id FROM {pre}user WHERE family_id='{$family_id}') AND state='1' ORDER BY weigh ASC");
        $allaccount = array();
        while ($row = $this->db->get_array()) {
            $allaccount[$row['id']] = $row;
        }
        $this->allaccount = $allaccount;
        return $allaccount;
    }

    //变更账户余额
    function update_user_account($account_id, $amount = 0, $memo = '', $user_id = null) {
        if ($amount == 0)
            return false;
        $user_id = !is_null($user_id) ? $user_id : $this->auth->user_id;
        $accountinfo = $this->get_account($account_id);
        $balance = $accountinfo ? $accountinfo['balance'] + $amount : 0;
        $type = $amount < 0 ? 'out' : 'in';
        $this->db->insert("{pre}account_log", array('user_id' => $user_id, 'account_id' => $account_id, 'amount' => $amount, 'balance' => $balance, 'createtime' => time(), 'memo' => $memo));
        $this->db->update("{pre}account", array('balance' => $balance), array('id' => $account_id));
        return true;
    }

    /**
     * 读取读写账户信息
     * @param int $id 
     * @return array
     */
    function get_account($id) {
        if (!$this->allaccount) {
            $this->allaccount = $this->load_user_account();
        }
        if (isset($this->allaccount[$id])) {
            return $this->allaccount[$id];
        } else {
            return array('id' => 0, 'name' => '未知');
        }
    }

    /**
     * 读取指定用户账户列表
     * @param int $user_id
     * @return array
     */
    function get_account_list($type = null, $user_id = null) {
        $type = is_array($type) ? $type : ($type ? array($type) : array());
        $user_id = !is_null($user_id) ? $user_id : $this->auth->user_id;
        $result = array();
        foreach ($this->allaccount as $m => $n) {
            if ($type && !in_array($n['type'], $type))
                continue;
            if ($n['user_id'] == $user_id) {
                $result[$n['id']] = $n;
            }
        }
        return $result;
    }

}